define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang"
    ],function(Util, Tool, SelectOrg, SearchBlock){

    var sysPath =  getServer() + "/static/app/cdqh/fourthgenerations";

    var qhGeoFourthgenerationsFormHtml = sysPath + "/views/qhGeoFourthgenerationsForm.html";
	var getQhGeoFourthgenerationsController = function(){
		return getServer() + "/qh/qhGeoFourthgenerationss";
	};

	/**
	 * 页面初始化
	 */
	var init = function(){
        initSearchBlock();
        queryBtnBind();
		createQhGeoFourthgenerationsGrid();
	};


	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
	};
	var queryBtnBind = function(){
        $("#btnSearch").click(function () {
            createQhGeoFourthgenerationsGrid();
        });
        $("#btnReset").click(function () {
			$("#ogrFidCondition").val("");
				$("#accCondition").val("");
			});
	};
	var createQhGeoFourthgenerationsGrid= function() {
        $("#qhGeoFourthgenerationsGrid").datagrid({
            url:getQhGeoFourthgenerationsController() + "/queryQhGeoFourthgenerationss",
			method:"GET",
            fitColumns: true,
            autoRowHeight: false,
            columns:[[
				{
					field:'ogrFid',
					title:'自增ID',
					width:'20',
					sortable: true,
					align:'center',
					formatter:function(value,rowData,rowIndex){
						return '<a href="#" onclick="_editQhGeoFourthgenerations(\'' + rowData.ogrFid + '\');"> '+rowData.ogrFid+' </a> '
					}
				},
				{
					field:'acc',
					title:'累加类型',
					width:'20',
					align:'center',
				}

            ]],
            toolbar: [
            // 	{
            //     iconCls: 'fa fa-plus-circle',
            //     text:"添加",
            //     handler: function(){
            //         addQhGeoFourthgenerations();
            //     }
            // },{
            //     iconCls: 'fa fa-trash-o',
            //     text:"删除",
            //     handler: function(){
            //         deleteQhGeoFourthgenerations();
            //     }
            // }
            ],
            queryParams:{
				ogrFid: $("#ogrFidCondition").val(),
				acc: $("#accCondition").val(),
            },
            pagination: true,
            pageSize: 10
        });
    };
	var formValidator = function(){
		$("#qhGeoFourthgenerationsForm").validate({
			rules : {
				shape : {
					required : true,
				},
				acc : {
					required : true,
				},
			},
			messages : {
				shape : {
					required : "区域不允许为空!",
				},
				acc : {
					required : "累加类型不允许为空!",
				},
			}
		});
	};
	var getQhGeoFourthgenerations = function(id){
		$.ajax({
			url: getQhGeoFourthgenerationsController() + "/"+id,
			type: "get",
			success: function (data) {
				Tool.deserialize("qhGeoFourthgenerationsForm", data);
			}
		});
	};

	var addQhGeoFourthgenerations = function () {
		var slidebar = Util.slidebar({
			url: qhGeoFourthgenerationsFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				formValidator();
				$("#saveBtn").on("click", function () {
					if($("#qhGeoFourthgenerationsForm").valid()){
						var data = Tool.serialize("qhGeoFourthgenerationsForm");
						$.ajax({
							url: getQhGeoFourthgenerationsController() ,
                            contentType:"application/json",
							data: JSON.stringify(data),
							type: "post",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
                                createQhGeoFourthgenerationsGrid();
							}
						});
					}
				});
			}
		});
	};
	window._editQhGeoFourthgenerations = function(qhGeoFourthgenerationsId) {
		var slidebar = Util.slidebar({
			url: qhGeoFourthgenerationsFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				formValidator();
				getQhGeoFourthgenerations(qhGeoFourthgenerationsId);
				$("#saveBtn").on("click", function () {
					if($("#qhGeoFourthgenerationsForm").valid()){
						var data = Tool.serialize("qhGeoFourthgenerationsForm");
						$.ajax({
							url: getQhGeoFourthgenerationsController(),
                            contentType:"application/json",
                            data: JSON.stringify(data),
							type: "put",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
								createQhGeoFourthgenerationsGrid();
							}
						});
					}
				});
			}
		});
	};
	var deleteQhGeoFourthgenerations = function() {
		var rows = $("#qhGeoFourthgenerationsGrid").datagrid("getSelections");
		if (rows == null || rows.length == 0) {
			Util.alert("请选择一行数据!");
			return;
		}
		Util.confirm("是否要删除选中的数据?", function() {
			var ids = "";
			$.each(rows, function(i, row){
				ids += row.ogrFid + ",";

			});
			ids = ids.substr(0,ids.length - 1);
			$.ajax({
				url: getQhGeoFourthgenerationsController() ,
				data: {
					ids : ids
				},
				type: "delete",
				success: function (data) {
					createQhGeoFourthgenerationsGrid();
				}
			});
		}, function() {
			return;
		});

	};

	return {
		init:init
	};
});
